import VueRouter from 'vue-router'
import { getToken, verifyToken, removeToken } from '@/utils/auth';

const router = new VueRouter({
    routes: [
        {
            path: "/",
            name: "admin",
            redirect: 'admin/index',
            component: () => import("../views/admin/Admin.vue"),
            meta: {
                isAuth: true,
            },
        },
        {
            path: "/admin",
            name: "index",
            redirect: 'admin/index',
            component: () => import("../views/admin/Admin.vue"),
            meta: {
                isAuth: true,
            },
            children: [
                {
                    path: "index",
                    name: "index",
                    component: () => import("../views/admin/Index.vue"),
                    meta: {
                        isAuth: true
                    }
                },
                {
                    path: "system",
                    name: "system",
                    component: () => import("../views/system/Index.vue"),
                    meta: {
                        isAuth: true,
                        controller: "system",
                    },
                    children: [
                        {
                            path: "authority",
                            name: "authority",
                            component: () => import("../views/system/authority/Index.vue"),
                            meta: {
                                isAuth: true,
                                controller: "system",
                            }
                        },
                        {
                            path: "roles",
                            name: "roles",
                            component: () => import("../views/system/role/Index.vue"),
                            meta: {
                                isAuth: true,
                                controller: "system",
                            }
                        },
                        {
                            path: "menu",
                            name: "menu",
                            component: () => import("../views/system/menu/Index.vue"),
                            meta: {
                                isAuth: true,
                                controller: "system",
                            }
                        }
                    ]
                }
            ]
        },
        {
            path: "/login",
            name: "login",
            component: () => import("../views/login/Login.vue"),
            meta: {
                isAuth: false
            }
        }
    ]
});

router.beforeEach((to, from, next) => {
    console.log(to);
    if (to.meta.isAuth) {
        if (getToken() !== null) {
            var response = verifyToken(getToken())
            // Promise类型 数据通过then获取
            response.then(res => {
                if (res.code == 10200) {
                    next();
                } else {
                    removeToken();
                    next('/login');
                }
            })
            next();
        } else {
            next('/login');
        }
    }
    next()
});

export default router;